package com.ctim.web.index;

import com.alibaba.fastjson.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * author： Wangwei
 * date： 2017/6/26
 * desc：
 */
@RequestMapping("/rest/jsonp")
@Controller
public class JSONPController {

    @RequestMapping("/index")
    public String index(Model model, HttpServletResponse response){
        Cookie cookie = new Cookie("token","12ee56ff");
        cookie.setMaxAge(Integer.MAX_VALUE);// 设置为30min
        cookie.setPath("/");
        cookie.setDomain("kccf.com");
        response.addCookie(cookie);
        model.addAttribute("title","快车财富首页");
        return "index";
    }

    @RequestMapping(value = "/getscript",method = RequestMethod.GET)
    @ResponseBody
    public Object getscript(@RequestParam String callbackFun, HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();//这样便可以获取一个cookie数组
        if (null==cookies) {
            System.out.println("没有cookie=========");
        } else {
            for(Cookie cookie : cookies){
                System.out.println("name:"+cookie.getName()+",value:"+ cookie.getValue());
            }
        }
        return callbackFun + "("+ JSONArray.toJSON(cookies)+")";
    }
}
